package com.anosine.gpsl.android.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.anosine.gpsl.android.R;
import com.anosine.gpsl.android.base.BaseActivity;
import com.anosine.gpsl.android.entity.Login;
import com.anosine.gpsl.android.entity.ModifyPwdInfo;
import com.anosine.gpsl.android.presenter.InitPwdPresenter;
import com.anosine.gpsl.android.ui.InitPwdView;
import com.anosine.gpsl.android.util.StringUtil;
import com.anosine.gpsl.android.util.ToastHelper;
import com.anosine.gpsl.android.util.UserInfoProxy;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.ViewUtils;

/**
 * @Title: ModifyPwdActivity
 * @Package com.anosine.gpsl.android.ui.fragment
 * @Description:
 * @author: sunny
 * @date: 2015-11-24
 * @version V1.0
 */
public class ModifyPwdActivity extends BaseActivity implements InitPwdView {
	/**
	 * @Title: BackOnClick(View v)
	 * @Description: 返回按钮的点击事件，点击关闭当前activity
	 * @param: View v
	 * @return: 无
	 * @throws
	 */
	@OnClick(R.id.button_left)
	private void BackOnClick(View v) {
		finish();
	}

	@ViewInject(R.id.content_tv_title)
	TextView title;
	@ViewInject(R.id.et_modify_password_old)
	EditText mEtPasswordOld;
	@ViewInject(R.id.et_modify_password_new)
	EditText mEtPasswordNew;
	@ViewInject(R.id.et_modify_password_repeat)
	EditText mEtPasswordNewRepeat;

	@OnClick(R.id.btn_modify_info_send)
	private void ChangeOnClick(View v) {
		clickBtnModify();
	}

	@Override
	public void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_modify_user_pwd);
		ViewUtils.inject(this);

	}

	@Override
	protected void initViews() {

		title.setText("修改密码");

	}

	private void clickBtnModify() {
		String pwdOld = mEtPasswordOld.getText().toString();
		String pwdNew = mEtPasswordNew.getText().toString();
		String pwdNewRepeat = mEtPasswordNewRepeat.getText().toString();
		if (StringUtil.isEmptyOrNull(pwdOld)) {
			ToastHelper.toast("请输入原来密码");
			return;
		}
		if (StringUtil.isEmptyOrNull(pwdNew)) {
			ToastHelper.toast("请输入新密码");
			return;
		}
		if (StringUtil.isEmptyOrNull(pwdNewRepeat)) {
			ToastHelper.toast("请输入确认新密码");
			return;
		}
		if (!pwdNew.equals(pwdNewRepeat)) {
			ToastHelper.toast("新密码与确认新密码不一致");
			return;
		}
		initActivityOfInitPwd(pwdOld, pwdNew);
	}

	private void initActivityOfInitPwd(String pwdOld, String pwdNew) {
		Login info = UserInfoProxy.getInstance().getLoginInfo();
		InitPwdPresenter presenter = new InitPwdPresenter(this);
		presenter.loadInitPwd(info.userMd5, pwdOld, pwdNew);
	}

	@Override
	public void setInitPwd(ModifyPwdInfo t) {
		if ("msg_login_pwdtrue".equals(t.message)) {
			ToastHelper.toast("修改密码成功!");
			ToastHelper.toast("修改密码成功");
			Login info = UserInfoProxy.getInstance().getLoginInfo();
			info.setPassWord(mEtPasswordNew.getText().toString());
			UserInfoProxy.getInstance().setLoginUserInfo(info);
			startActivity(new Intent(getApplication(), LoginActivity.class));
			finish();
		} else if ("msg_login_userpassflase".equals(t.message)) {
			ToastHelper.toast("旧密码输入错误!");
		} else if ("msg_login_pwdfalse".equals(t.message)) {
			ToastHelper.toast("修改密码失败!");
		}

	}

}
